﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SmtpTestServer.UI
{
    class FileWatcherUtility
    {
	    internal static event EventHandler<FileCreatedEventArgs> FileCreatedEvent;
	    internal static event EventHandler<FileDeletedEventArgs> FileDeletedEvent;

		public FileWatcherUtility()
		{
			string filePathToWatch = Properties.Settings.Default.MessageDirectory;

			FileSystemWatcher WatchFile = new FileSystemWatcher(filePathToWatch, "*.eml");

			WatchFile.Created += new FileSystemEventHandler(this.FileCreated);
			
			WatchFile.Deleted += new FileSystemEventHandler(this.FileDeleted);
			WatchFile.EnableRaisingEvents = true;
		}

		public void FileCreated(object sender, FileSystemEventArgs e)
		{
			OnFileCreated(e.FullPath);
		}

		public void FileDeleted(object sender, FileSystemEventArgs e)
		{
			OnFileDeleted(e.FullPath);
		}

		public static void OnFileCreated(string fileNameWithFullPath)
		{
			if (string.IsNullOrEmpty(fileNameWithFullPath) == false)
			{
				FileCreatedEvent(fileNameWithFullPath, new FileCreatedEventArgs(fileNameWithFullPath));
			}
		}

		public static void OnFileDeleted(string fileNameWithFullPath)
		{
			if (string.IsNullOrEmpty(fileNameWithFullPath) == false)
			{
				FileDeletedEvent(fileNameWithFullPath, new FileDeletedEventArgs(fileNameWithFullPath));
			}
		}
	}
    
	class FileCreatedEventArgs : EventArgs
	{
		public string FileNameWithFullPath;

		public FileCreatedEventArgs(string fileNameWithFullPath)
		{
		    FileNameWithFullPath = fileNameWithFullPath;
		}
	}

	class FileDeletedEventArgs : EventArgs
	{
		public string FileNameWithFullPath;

		public FileDeletedEventArgs(string fileNameWithFullPath)
		{
		    FileNameWithFullPath = fileNameWithFullPath;
		}
	}
}
